import { getlist, insert, updates, delet } from "../api/msbd"

export default {
  namespaced: true,
  state: {
    tableData: [],
    classify: [],
    sort: "desc",
    orderby: 'id',
    currentPage: 1,
    pageSize: 5,
    total: 0,
    searchVal: ""
  },

  mutations: {
    SET_DATA(state, res) {
      // console.log(res.data.classify)
      state.tableData = res.data.data
      state.classify = res.data.classify
      state.total = res.data.total[0].total
    },
    // 当前页数
    SET_PAGE(state, page) {
      state.currentPage = page
    },
    // 每页项数
    SET_PAGSIZE(state, val) {
      state.pageSize = val
    },
    // 排序
    SET_SORT(state, ele) {
      if (ele.order == 'descending') {
        state.sort = 'desc'
        state.orderby = ele.porp
      } else if (ele.order == 'ascending') {
        state.sort = 'asc'
        state.orderby = ele.porp
      } else {
        state.sort = 'desc'
        state.orderby = 'id'
      }
    },
    // 搜索
    SET_SEARCH(state, val) {
      state.searchVal = val
    }
  },

  actions: {
    // 渲染
    async getlist({ commit, state }) {
      let r = await getlist({
        sort: state.sort,
        orderby: state.orderby,
        currentPage: state.currentPage - 1,
        pageSize: state.pageSize,
        searchVal: state.searchVal
      })
      // console.log(r)
      commit("SET_DATA", r)
    },
    // 新增确认
    async insert({ dispatch }, data) {
      // console.log(data)
      await insert(data);
      dispatch('getlist')
    },
    // 修改确认
    async updates({ dispatch }, data) {
      await updates(data)
      dispatch('getlist')
    },
    // 删除
    async delet({ dispatch }, id) {
      await delet(id)
      dispatch('getlist')
    }
  },

  getters: {

  },

}